package com.moneywelcome.riskassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moneywelcome.api.UserGradeApi;
import com.moneywelcome.dto.UserGradeInfo;
import com.moneywelcome.results.Result;
import com.moneywelcome.riskassembly.model.UserGrade;
import com.moneywelcome.riskassembly.service.UserGradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserGradeApiImpl implements UserGradeApi {
    @Autowired
    private UserGradeService userGradeService;


    @Override
    public Result saveGrade(Integer userId, Integer score) {
        UserGrade userGrade = new UserGrade();
        userGrade.setUserId(userId);
        userGrade.setUserGrade(score);
        return userGradeService.save(userGrade)?Result.success(score):Result.fail();
    }

    @Override
    public Result<UserGradeInfo> getGradeByUser(Integer userId) {
        UserGrade one = userGradeService.getOne(new QueryWrapper<UserGrade>().eq("user_id", userId));
        return null==one?Result.fail():Result.success(BeanUtil.copyProperties(one,UserGradeInfo.class));
    }
}
